/**
 * @package MusicPlayer
 * @version 1.0
 * @author 易航
 * @link http://blog.bri6.cn
 * @giant APlayer
*/

class MusicPlayer {

	constructor(options) {

		/* 播放器 */
		this.PLAYER = null

		/* 播放器配置 */
		this.OPTIONS = options

		return this.init()
	}

	/**
	 * 初始化播放器
	 */
	init() {
		// 优化全部音乐信息
		this.setMusic()

		// 创建音乐播放器
		this.createPlayer()

		// 监听播放器事件
		this.listen()

		// 换回之前播放的音乐
		this.initStorageMusic()

		return this.PLAYER
	}

	/**
	 * 监听播放器事件
	 */
	listen() {
		if (this.OPTIONS.storage) {
			setInterval(() => {
				this.storageMusic()
			}, 1000);
			document.getElementsByTagName('a').onclick = () => {
				this.storageMusic()
			}
			window.onbeforeunload = () => {
				this.storageMusic()
			}
		}
		this.PLAYER.on('loadeddata', () => {
			this.OPTIONS['autotheme'] ? this.autoTheme(this.PLAYER.list.index) : null;
			if ('mediaSession' in navigator) {
				let music = this.PLAYER.list.audios[this.PLAYER.list.index];
				navigator.mediaSession.metadata = new MediaMetadata({
					title: music.name,
					artist: music.artist,
					// album: '专辑名',
					artwork: [
						{src: music.pic},
						{src: music.cover},
						// { src: '封面图片URL', sizes: '256x256', type: 'image/png' },
						// { src: '封面图片URL', sizes: '512x512', type: 'image/png' }
					]
				});
			}
		})
		this.PLAYER.on('error', () => {
			console.log(this.PLAYER.list.audios[this.PLAYER.list.index]);
			this.PLAYER.skipForward();
			this.PLAYER.seek(0);
		})
	}

	/**
	 * 创建播放器
	 */
	createPlayer() {
		this.PLAYER = new APlayer(this.OPTIONS)
	}

	/**
	 * 初始化存储音乐信息
	 */
	initStorageMusic() {
		if (!this.OPTIONS.storage) {
			return
		}
		let storage_music = this.getMusic(this.OPTIONS.storage);
		if (!storage_music) {
			return
		}
		let music = this.PLAYER.list.audios[storage_music.index]
		if (!music) {
			return
		}
		if (
			(this.PLAYER.list.audios.length != 1) &&
			(music['url'] != storage_music['url']) &&
			(music['artist'] != storage_music['artist'] && music['name'] != storage_music['name'])
		) {
			return
		}
		this.PLAYER.list.switch(storage_music.index);
		var seek_loadedmetadata = true;
		this.PLAYER.on('loadedmetadata', () => {
			if (!seek_loadedmetadata) {
				return
			}
			this.PLAYER.seek(storage_music.time);
			var time = this.PLAYER.audio.currentTime;
			if (time <= 0) {
				// alert('垃圾浏览器，音频的loadedmetadata事件都监控不准');
				setTimeout(() => {
					this.PLAYER.seek(storage_music.time);
				}, 50);
			}
			seek_loadedmetadata = false;
		})
	}

	/**
	 * 获取存储的音乐信息
	 * @return object|null
	 */
	getMusic(storage = null) {
		if (localStorage.getItem('music_player')) {
			var data = JSON.parse(localStorage.getItem('music_player'));
			if (storage) {
				return data[storage] ? data[storage] : null
			}
			return data
		}
		return null
	}

	/**
	 * 存储音乐
	 */
	storageMusic() {
		if (!this.OPTIONS.storage) {
			return;
		}
		let music = this.PLAYER.list.audios[this.PLAYER.list.index];
		music.time = this.PLAYER.audio.currentTime;
		music.index = this.PLAYER.list.index;
		let storage = this.getMusic();
		let data = (storage ? storage : new Object);
		data[this.OPTIONS.storage] = music;
		localStorage.setItem('music_player', JSON.stringify(data))
	}

	/**
	 * 音乐播放器自动主题色
	 */
	autoTheme(index) {
		if (this.PLAYER.list.audios[index]) {
			if (!this.PLAYER.list.audios[index].theme) {
				let xhr = new XMLHttpRequest()
				xhr.open('GET', this.PLAYER.list.audios[index].cover, true)
				xhr.responseType = 'blob'
				xhr.send()
				xhr.onload = () => {
					var coverUrl = URL.createObjectURL(xhr.response)
					let image = new Image()
					image.onload = () => {
						let colorThief = new ColorThief()
						let color = colorThief.getColor(image)
						this.PLAYER.theme(`rgb(${color[0]}, ${color[1]}, ${color[2]})`, index)
						URL.revokeObjectURL(coverUrl)
					}
					image.src = coverUrl
				}
			}
		}
	}

	/**
	 * 优化音乐信息
	 * @return object
	 */
	setMusic() {
		let music = this.OPTIONS.audio
		for (let key in music) {
			// 音频名称
			if (!music[key]['name']) {
				music[key]['name'] = music[key]['title'] ? music[key]['title'] : '歌曲'.key
			}
			// 音频作者
			if (!music[key]['artist']) {
				music[key]['artist'] = music[key]['author'] ? music[key]['author'] : '无信息'
			}
			// 音频歌词
			if (!music[key]['lrc']) {
				music[key]['lrc'] = '[00:00.000] 暂无歌词'
			}
			// 音频封面
			if (!music[key]['cover']) {
				music[key]['cover'] = music[key]['pic'] ? music[key]['pic'] : ''
			}
		}
		this.OPTIONS.audio = music
		return music
	}
}